<?php

namespace app\api\controller;


use addons\epay\library\Service;
use addons\recharge\library\Order;
use addons\recharge\model\MoneyLog;
use app\api\model\Banner;
use app\api\model\ScoreLog;
use app\common\controller\Api;
use think\Exception;


/**
 * 充值接口
 */
class Recharge extends Api
{
    protected $noNeedLogin = ['epay'];

    protected $noNeedRight = ['*'];

    //充值套餐列表
    public function recharge()
    {
        //\think\Log::record(123);
        $list = \app\admin\model\Recharge::order('money asc')->field('recharge_id,money,score')->select();
        $this->success('',$list);
    }

    public function submit()
    {
        //$money = $this->request->request('money/f');

        $recharge_id = input('recharge_id',0);
        $money = input('money',0);
        if ($money <= 0) {
            $this->error('充值金额不正确');
        }
        /*$config = get_addon_config('recharge');
        if (isset($config['minmoney']) && $money < $config['minmoney']) {
            $this->error('充值金额不能低于' . $config['minmoney'] . '元');
        }*/

        try {
            $response = Order::newsubmit($recharge_id,$money, 'wechat');
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$response);
    }

    /**
     * 创建订单并发起支付请求
     */
    public function oldsubmit()
    {
        $info = get_addon_info('epay');
        if (!$info || !$info['state']) {
            $this->error('请在后台插件管理安装微信支付宝整合插件后重试');
        }
        $money = $this->request->request('money/f');
        $recharge_id = input('recharge_id',0);
        if ($money <= 0) {
            $this->error('充值金额不正确');
        }
       /* $config = get_addon_config('recharge');
        if (isset($config['minmoney']) && $money < $config['minmoney']) {
            $this->error('充值金额不能低于' . $config['minmoney'] . '元');
        }*/
        $user = \app\admin\model\User::get($this->auth->id);
        try {
            $response = Order::submit($recharge_id,$money,'wechat','miniapp',$user->wechat_open_id);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$response);
    }

    public function epay()
    {
        \think\Log::record('回调内容');
        $data = input();
        \think\Log::record($data);
        $res = \addons\recharge\library\Order::settle($data['cusorderid'], $data['trxamt']/100,$data['trxid']);
        $this->success('',$res);
    }

    /**
     * 企业支付通知和回调
     */
    public function oldepay()
    {
        $type = $this->request->param('type');
        $paytype = $this->request->param('paytype');
        if ($type == 'notify') {
            $pay = Service::checkNotify($paytype);
            if (!$pay) {
                echo '签名错误';
                return;
            }
            //判断是V2还是V3
            $data = Service::isVersionV3() ? $pay->callback() : $pay->verify();

            try {
                //微信支付V3返回和V2不同
                if (Service::isVersionV3() && $paytype === 'wechat') {
                    $data = $data['resource']['ciphertext'];
                    $data['total_fee'] = $data['amount']['total'];
                }

                $payamount = $paytype == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
                \addons\recharge\library\Order::settle($data['out_trade_no'], $payamount);
            } catch (Exception $e) {
                \think\Log::record("回调逻辑处理错误:" . $e->getMessage(), "error");
            }

            //下面这句必须要执行,且在此之前不能有任何输出
            if (Service::isVersionV3()) {
                return $pay->success()->getBody()->getContents();
            } else {
                return $pay->success()->send();
            }

        } else {
            $this->success("请返回网站查看支付状态!");
        }
    }

    //余额明细
    public function moneylog()
    {
        $start_time = input('start_time','');
        $end_time = input('end_time','');

        $where = ['user_id' => $this->auth->id];

        if ($start_time&&$end_time){
            $where['createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
        }

        $moneyloglist = MoneyLog::where($where)
            ->order('id desc')
            ->paginate(10);
        $this->success('',$moneyloglist);
    }

    //积分明细
    public function scorelog()
    {
        $start_time = input('start_time','');
        $end_time = input('end_time','');
        $type = input('type','all');

        $where = ['user_id' => $this->auth->id];

        if ($type!='all'){
            if ($type==0){
                $where['score'] = ['<',0];
            } else {
                $where['score'] = ['>',0];
            }
        }

        if ($start_time&&$end_time){
            $where['createtime'] = ['between', [strtotime($start_time), strtotime($end_time)]];
        }

        $moneyloglist = ScoreLog::where($where)
            ->order('id desc')
            ->paginate(10);
        $this->success('',$moneyloglist);
    }

}